<?php

namespace App\Console\Commands;

use App\Http\Model\CrossRoomPkRecord;
use App\Http\Model\Room;
use App\Http\Model\Union;
use App\Http\Services\CrossRoomPkService;
use App\Http\Services\MsgServices;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class CrossRoomOutTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'CrossRoomPkOutTime';

    protected $crossRoomPkTable ='cross_room_pk_record';
    protected $roomModel;
    protected $service_msg;
    protected $crossRoomService;
    protected $crossRoomPkModel;

    protected $crossRoomWinnerRoomCount = 'CROSS:ROOM:PK:WINNER:';//房间胜利次数 CROSS:ROOM:PK:WINNER:{roomid} => {count}
    protected $crossRoomPkRedisList = 'CROSS:ROOM:PK:ALLIST'; //所有玩PK的房间列表 set 


    protected $log = 'cross_room_pk_out';//日志
    protected $error_log = 'error_cross_room_pk_out';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '邀请超时自动拒绝';


    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->crossRoomPkModel = new CrossRoomPkRecord();
        $this->roomModel = new Room();
        $this->service_msg = new MsgServices();
        $this->crossRoomService = new CrossRoomPkService();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $allData = DB::table($this->crossRoomPkTable)->where(['send_status'=>0,'pk_status'=>0])->get();
        $allData = objToArray($allData);
        do{
            if(empty($allData)){
                write_log($this->log,'INFO 无数据',$allData);
                break;
            }
            //处理数据
            $now = Carbon::now()->toDateTimeString();
            foreach($allData as $k=>$v){
                //时长对比
                $diffTime = Carbon::parse($now)->diffInMinutes($v['created_at']);
                $diffSecond = Carbon::parse($now)->diffInSeconds($v['created_at']);
                //定向2分后 超时
                if($diffTime >= 2 ){
                    $update = [
                        'to_user_id' => 11,
                        'pk_status' => 0,
                        'send_status' => 3, //1=接受 2=拒绝 3=系统拒绝
                        'updated_at' => $now,
                    ];
                    $this->crossRoomPkModel->upRecordPk($update,$v['id']);
                    //查询接收者房间信息
                    $roomInfo = $this->roomModel->get_room($v['to_room_id']);
                    //发送消息
                    $msg = $this->service_msg->confirm_cross_pk_msg($v['from_room_id'], $roomInfo, 3, '00:00:00', '00:00:00', $v['id'], $v['type']);
                    write_log($this->log,'INFO 发送通知回调',['data'=>$v,'status'=>$msg]);
                    //自己房间发送接收状态
                    $this->service_msg->our_confirm_cross_pk_msg($v['to_room_id'], $roomInfo, 3, '00:00:00', '00:00:00', $v['id'], $v['type']);
                    write_log($this->log,'INFO 我方发送通知回调',['data'=>$v,'status'=>$msg]);
                }elseif($diffSecond >= 9 && $v['type'] == 2){
                    //随机匹配
                    $this->crossRoomService->sendRandPkMsg($v['from_room_id'],$v['from_user_id'], 2, $v['id']);
                }
            }
        }while(false);

        //系统查看PK是否结束
        $allData = DB::table($this->crossRoomPkTable)->where(['send_status'=>1,'pk_status'=>4])->get();
        $allData = objToArray($allData);
        if(!empty($allData)){
            foreach($allData as $k=>$v){
                $sendId = $v['id'];
                //处理数据
                $now = Carbon::now()->toDateTimeString();
                $diffTime = Carbon::parse($now)->diffInMinutes($v['pk_end_time']);
                //查看PK时间是否小于当前时间自动结算
                if($diffTime >= 32){
                   $this->crossRoomService->endPk($sendId,0,$v['from_room_id'], $v['from_user_id']);
                }
            }
        }

    }
}